package com.dw.widget.slideshow2.utils.log;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.content.Context;
import android.os.Environment;

public class FileLog {

	private final String TAG = "FILELOG";
	
	private static final String DEBUG_TAG = "debug";
	private static final String WARNING_TAG = "warning";
	private static final String ERROR_TAG = "error";
	private static final String INFO_TAG = "info";
	
	private static Context mContext;
	private static FileLog mInstance;
	
	public static FileLog getInstance(Context context)
	{
		if(context == null)
		{
			return null;
		}
		
		mContext = context;
		
		if(mInstance == null)
		{
			mInstance = new FileLog();
		}
		
		return mInstance;
	}
	
	/**
	 * 保存调试信息
	 * @param tag
	 * @param info
	 */
	public void d(String tag, String info)
	{
		tag = tag == null ? "" : tag;
		info = info == null ? "" : info;
		
		String time = getCurrentTime();
		String log = String.format("%1$s  %2$s  %3$s %4$s\r\n", time, DEBUG_TAG, tag, info);
		writeLog(log);
	}
	
	/**
	 * 保存警告信息
	 * @param tag
	 * @param info
	 */
	public void w(String tag, String info)
	{
		tag = tag == null ? "" : tag;
		info = info == null ? "" : info;
		
		String time = getCurrentTime();
		String log = String.format("%1$s  %2$s  %3$s %4$s\r\n", time, WARNING_TAG, tag, info);
		writeLog(log);
	}
	
	/**
	 * 保存错误信息
	 * @param tag
	 * @param info
	 */
	public void e(String tag, String info)
	{
		tag = tag == null ? "" : tag;
		info = info == null ? "" : info;
		
		String time = getCurrentTime();
		String log = String.format("%1$s  %2$s  %3$s %4$s\r\n" + System.getProperty("line.separator"), time, ERROR_TAG, tag, info);
		writeLog(log);
	}
	
	/**
	 * 保存一般信息
	 * @param tag
	 * @param info
	 */
	public void i(String tag, String info)
	{
		tag = tag == null ? "" : tag;
		info = info == null ? "" : info;
		
		String time = getCurrentTime();
		String log = String.format("%1$s  %2$s  %3$s %4$s\r\n", time, INFO_TAG, tag, info);
		writeLog(log);
	}
	
	/**
	 * 获取日志文件全路径
	 * @return
	 */
	public String getLogFileName()
	{
		StringBuilder filePath = new StringBuilder();
		
		String sdStatus = Environment.getExternalStorageState();
		if(sdStatus.equals(Environment.MEDIA_MOUNTED))   // sd卡存在
		{
			filePath.append(Environment.getExternalStorageDirectory().getAbsolutePath());
		}
		else   // sd卡不存在则存放在内存中
		{
			filePath.append(mContext.getFilesDir().getAbsolutePath());
		}
		
		Date time = new Date();
		String date = sdf.format(time);

		// 文件名为 com.jsict.project_20080808.txt
		filePath.append(File.separator);
		filePath.append(String.format("%1$s_%2$s.txt", mContext.getApplicationInfo().packageName, date));
		
		return filePath.toString();
	}
	private  SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");//日志名称格式
	/**
	 * 获取日志文件名称
	 * @return
	 */
	public String getLogName()
	{
		StringBuilder filePath = new StringBuilder();
		
		
		Date time = new Date();
		String date = sdf.format(time);
		
		// 文件名为 com.jsict.project_20080808.txt
		//filePath.append(File.separator);
		filePath.append(String.format("%1$s_%2$s.txt", mContext.getApplicationInfo().packageName, date));
		
		return filePath.toString();
	}
	
	/**
	 * 获取当前时间 格式为2008-08-08 08:08:08
	 * @return
	 */
	private String getCurrentTime()
	{
		Calendar c = Calendar.getInstance();
		int year = c.get(Calendar.YEAR);
		int month = c.get(Calendar.MONTH);
		int day = c.get(Calendar.DAY_OF_MONTH);
		int hour = c.get(Calendar.HOUR);
		int min = c.get(Calendar.MINUTE);
		int sec = c.get(Calendar.SECOND);
		
		String time = String.format("%1$d-%2$d-%3$d %4$d:%5$d:%6$d", year, month + 1, day, hour, min, sec);
		
		return time;
	}
	
	/**
	 * 获取输入文件Stream，如果文件不存在先创建
	 * @param path
	 * @return
	 */
	private OutputStream getStream(String path)
	{
		OutputStream stream = null;
		
		try
		{
			File fLog = new File(path);
			if(fLog.exists() == false)
			{
				fLog.getParentFile().mkdirs();
				fLog.createNewFile();
			}
			
			stream = new FileOutputStream(path, true);
		}
		catch(Exception ex)
		{
			ex.printStackTrace();
		}
		
		return stream;
	}
	
	/**
	 * 写日志
	 * @param log
	 */
	private void writeLog(String log)
	{
		String logPath = getLogFileName();
		OutputStream stream = getStream(logPath);
		if(stream == null)
		{
			android.util.Log.e(TAG, "write file log FAILED!(could not get stream)");
			return;
		}
		
		try {
			stream.write(log.getBytes());
			stream.flush();
		} 
		catch (IOException e) {
			
			android.util.Log.e(TAG, "write file log FAILED!(write to stream exception)");
			e.printStackTrace();
		}
		finally{
			try{
				stream.close();
			}
			catch(Exception e)
			{
				e.printStackTrace();
			}
		}
	}
}
